package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.entitys.Member;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.user.intercepter.TokenIntercepter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：王李轶群
 * @description：TODO
 * @date ：5/19/22 9:46 AM
 */
@RestController
public class OrderController {
    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    @PostMapping("/shopping/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest,HttpServletRequest request) {
        String user_info = request.getHeader("user_info");
        Member user = JSON.parseObject(user_info, Member.class);
        Long id = user.getId();
        createOrderRequest.setUserId(id);
        CreateOrderResponse orderCreate = orderCoreService.createOrder(createOrderRequest);
        ResponseData<String> stringResponseData = new ResponseData<>();
        stringResponseData.setResult(orderCreate.getOrderId());
        stringResponseData.setCode(200);
        stringResponseData.setMessage("success");
        stringResponseData.setSuccess(true);
        return stringResponseData;
    }

    @GetMapping("/shopping/order")
    public ResponseData<OrderListResponse> orderList(OrderListRequest orderListRequest, HttpServletRequest request) {
        String token = request.getHeader("user_info");
        Member user = JSON.parseObject(token, Member.class);
        String name = user.getUsername();
        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);
        ResponseData<OrderListResponse> orderListResponseResponseData = new ResponseData<>();
        orderListResponseResponseData.setResult(orderListResponse);
        orderListResponseResponseData.setSuccess(true);
        orderListResponseResponseData.setMessage("success");
        orderListResponseResponseData.setCode(200);
        return orderListResponseResponseData;
    }

    @GetMapping("/shopping/order/{id}")
    public ResponseData orderDetail(@PathVariable("id") Long id, HttpServletRequest servletRequest){

        //提取UserInfo
        String user_info = servletRequest.getHeader("user_info");
        Member user = JSON.parseObject(user_info, Member.class);
        Long uid = user.getId();

        //调用service层方法
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(id.toString());
        OrderDetailResponseVO response = orderQueryService.getOrderDetail(orderDetailRequest);

        //传入UserID
        response.setUserId(uid);

        //返回response
        if(response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/shopping/order/{id}")
    public ResponseData orderDelete(@PathVariable("id") Long id, HttpServletRequest servletRequest) {

        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id.toString());

        //调用service层方法
        DeleteOrderResponse response = orderCoreService.deleteOrder(request);

        //返回response
        if(response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
